﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lacrima.Framework.Infrastructure
{
    /// <summary>
    /// 坐标系类
    /// </summary>
    public class Coordinate
    {
        /// <summary>
        /// 坐标转换，将十进制度转换为度时分秒的形式
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>System.String.</returns>
        public static string ToDegree(double source)
        {
            if (!double.IsNaN(source))
            {
                double degree = Math.Abs(source);
                double minute = Arithmetic.GetNumberLeftOffsetTwoDigtal(degree) * 60 / 100;
                double second = Arithmetic.GetNumberLeftOffsetTwoDigtal(minute) * 60;
                string coordinate = string.Format("{0:00}{1:00}{2:0000}", (int)degree, (int)minute, Math.Round(second, 0));
                return coordinate;
            }
            return string.Empty;
        }

        /// <summary>
        /// 将十进制纬度转换为坐标制纬度
        /// 方法参照：http://blog.sina.com.cn/s/blog_7b0807af0101lh3n.html
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>System.String.</returns>
        public static string ToLatitude(double source)
        {
            string latitude = string.Format("{0}{1}", GetLatitudeDirection(source), ToDegree(source));
            return latitude;
        }

        /// <summary>
        /// 将十进制经度转换为坐标制经度
        /// 方法参照：http://blog.sina.com.cn/s/blog_7b0807af0101lh3n.html
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>System.String.</returns>
        public static string ToLongitude(double source)
        {
            string direction = Arithmetic.IsPositive(source) ? "E" : "W";
            string longitude = string.Format("{0}{1}", direction, ToDegree(source));
            return longitude;
        }

        /// <summary>
        /// 获取纬度方向
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>System.String.</returns>
        public static string GetLatitudeDirection(double source)
        {
            string direction = Arithmetic.IsPositive(source) ? "N" : "S";
            return direction;
        }

        /// <summary>
        /// 获取经度方向
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>System.String.</returns>
        public static string GetLongitudeDirection(double source)
        {
            string direction = Arithmetic.IsPositive(source) ? "E" : "W";
            return direction;
        }
    }
}
